{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import cPickle as pickle\n",
    "from IPython.display import display\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.backends.backend_pdf import PdfPages\n",
    "\n",
    "# from matplotlib import rc\n",
    "# rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})\n",
    "## for Palatino and other serif fonts use:\n",
    "#rc('font',**{'family':'serif','serif':['Palatino']})\n",
    "# rc('text', usetex=True)\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "remap_dict = {\n",
    "    ('unmeasure', 'blur'): 'unmeasure-blur',\n",
    "    ('unmeasure', 'inpaint-tv') : 'unmeasure-inpaint-tv',\n",
    "    ('baseline', 'None') : 'ignore',\n",
    "    ('ambient', 'None') : 'AmbientGAN (ours)',\n",
    "    ('unmeasure', 'wiener'): 'unmeasure-weiner',\n",
    "    'pad_rotate_project': 'Pad-Rotate-Project',\n",
    "    'pad_rotate_project_with_theta': r'Pad-Rotate-Project-$\\theta$',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def save_plot(save_path):\n",
    "    pdf = PdfPages(save_path)\n",
    "    pdf.savefig(bbox_inches='tight')\n",
    "    pdf.close()   \n",
    "    \n",
    "def errorbar(x, y, std):\n",
    "    (_, caps, _) = plt.errorbar(x, y, yerr=1.96*std,\n",
    "                                marker='o', markersize=5, capsize=5,\n",
    "                                alpha=0.8, linewidth=2)\n",
    "    for cap in caps:\n",
    "        cap.set_markeredgewidth(1)\n",
    "\n",
    "def plot(plot_spec, df):\n",
    "    groups = df.groupby(plot_spec['group_by'])\n",
    "    legends = []\n",
    "    x_var = plot_spec['x_var']\n",
    "    y_var = plot_spec['y_var']\n",
    "    std_var = plot_spec['std_var']\n",
    "    \n",
    "    plt.figure(figsize=[6, 4])\n",
    "    \n",
    "    for key in sorted(groups.groups):\n",
    "        val = groups.groups[key]\n",
    "        group_df = df.loc[val, :]\n",
    "        cols = [x_var, y_var, std_var]\n",
    "        group_df.sort_values(by=[x_var], inplace=True)\n",
    "        x = np.array(group_df[x_var])\n",
    "        y = np.array(group_df[y_var])\n",
    "        if std_var is not None:\n",
    "            std = np.array(group_df[std_var])\n",
    "            errorbar(x, y, std)\n",
    "        else:\n",
    "            plt.plot(x, y, '-o', alpha=0.8, linewidth=2)\n",
    "        legend = remap_dict[key]\n",
    "        legends.append(legend)\n",
    "        \n",
    "    ## Prettify\n",
    "    # axis\n",
    "    plt.gca().set_ylim(bottom=0)\n",
    "    # plt.gca().set_xscale(\"log\", nonposx='clip')\n",
    "    # plt.gca().set_xlim([-0.1, 1.1])\n",
    "\n",
    "    # labels, ticks, titles\n",
    "    # ticks = [10, 25, 50, 100, 200, 300, 400, 500, 750]\n",
    "    # labels = [10, 25, 50, 100, 200, 300, 400, 500, 750]\n",
    "    # plt.xticks(ticks, labels, rotation=90)\n",
    "    plt.xlabel(plot_spec['x_label'], fontsize=14)\n",
    "    plt.ylabel(plot_spec['y_label'], fontsize=14)\n",
    "\n",
    "    # Legends\n",
    "    plt.legend(legends, fontsize=12.5, loc=3)\n",
    "    # plt.ylim([0, 0.1])\n",
    "    \n",
    "    plt.title(plot_spec['title'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def convert_df(df, convert_spec):\n",
    "    for colname in convert_spec['float']:\n",
    "        df.loc[:, colname] = df[colname].apply(np.float32)\n",
    "    return df\n",
    "\n",
    "def filter_df(df, filter_spec):\n",
    "    df = df[filter_spec['cols']]\n",
    "    filter_ = True\n",
    "    for colname, values in filter_spec['in'].iteritems():\n",
    "        filter_ = filter_ & df[colname].isin(values)\n",
    "    df = df[filter_]\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def main(df_filepath, convert_spec, filter_spec, plot_spec):\n",
    "    with open(df_filepath, 'rb') as df_pkl_file:\n",
    "        df = pickle.load(df_pkl_file)\n",
    "    df = convert_df(df, convert_spec)\n",
    "    df = filter_df(df, filter_spec)    \n",
    "    display(df)\n",
    "    plot(plot_spec, df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "df_filepath = './results/df_mnist.pkl'\n",
    "\n",
    "convert_spec = {\n",
    "    'float': [\n",
    "        'drop_prob',\n",
    "        'm_inception_score_mean', \n",
    "#         'm_inception_score_std',\n",
    "    ]        \n",
    "}\n",
    "\n",
    "filter_spec = {\n",
    "    'cols' : ['measurement_type', \n",
    "              'train_mode', \n",
    "              'drop_prob',\n",
    "              'unmeasure_type', \n",
    "              'm_inception_score_mean',\n",
    "#               'm_inception_score_std',\n",
    "              'model_type',\n",
    "             ],\n",
    "    'in': {\n",
    "        'measurement_type': ['drop_independent'],\n",
    "        'model_type' : ['wgangp'],\n",
    "    }\n",
    "}\n",
    "plot_spec = {\n",
    "    'group_by': ['train_mode', 'unmeasure_type'],\n",
    "    # change these to lambda if needed\n",
    "    'x_var': 'drop_prob',\n",
    "    'y_var': 'm_inception_score_mean',\n",
    "#     'std_var': 'm_inception_score_std',\n",
    "    'std_var': None,\n",
    "    'x_label': 'Block probability (p)',\n",
    "    'y_label': 'Inception score',\n",
    "    'title': 'MNIST, Block-Pixels',\n",
    "}\n",
    "\n",
    "main(df_filepath, convert_spec, filter_spec, plot_spec)\n",
    "fig_savepath = './results/plots/mnist_drop_independent.pdf'\n",
    "save_plot(fig_savepath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_filepath = './results/df_mnist.pkl'\n",
    "\n",
    "convert_spec = {\n",
    "    'float': ['additive_noise_std', 'm_inception_score_mean']        \n",
    "}\n",
    "\n",
    "filter_spec = {\n",
    "    'cols' : ['measurement_type', \n",
    "              'train_mode', \n",
    "              'additive_noise_std',\n",
    "              'unmeasure_type', \n",
    "              'm_inception_score_mean'\n",
    "             ],\n",
    "    'in': {\n",
    "        'measurement_type': ['blur_addnoise']\n",
    "    }\n",
    "}\n",
    "plot_spec = {\n",
    "    'group_by': ['train_mode', 'unmeasure_type'],\n",
    "    # change these to lambda if needed\n",
    "    'x_var': 'additive_noise_std',\n",
    "    'y_var': 'm_inception_score_mean',\n",
    "    'std_var': None,\n",
    "    'x_label': 'Noise standard deviation ($\\sigma$)',\n",
    "    'y_label': 'Inception score',\n",
    "    'title': 'placeholder',\n",
    "}\n",
    "\n",
    "main(df_filepath, convert_spec, filter_spec, plot_spec)\n",
    "fig_savepath = './results/plots/mnist_blur_noise.pdf'\n",
    "save_plot(fig_savepath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_filepath = './results/df_mnist.pkl'\n",
    "\n",
    "convert_spec = {\n",
    "    'float': ['additive_noise_std', 'm_inception_score_mean']        \n",
    "}\n",
    "\n",
    "filter_spec = {\n",
    "    'cols' : ['measurement_type', \n",
    "              'train_mode', \n",
    "              'additive_noise_std',\n",
    "              'unmeasure_type', \n",
    "              'm_inception_score_mean',\n",
    "              'num_angles',\n",
    "             ],\n",
    "    'in': {\n",
    "        'measurement_type': [\n",
    "            'pad_rotate_project',\n",
    "            'pad_rotate_project_with_theta',\n",
    "        ]\n",
    "    }\n",
    "}\n",
    "plot_spec = {\n",
    "    'group_by': ['measurement_type'],\n",
    "    # change these to lambda if needed\n",
    "    'x_var': 'num_angles',\n",
    "    'y_var': 'm_inception_score_mean',\n",
    "    'std_var': None,\n",
    "    'x_label': 'Number of angles',\n",
    "    'y_label': 'Inception score',\n",
    "    'title': 'placeholder',\n",
    "}\n",
    "\n",
    "main(df_filepath, convert_spec, filter_spec, plot_spec)\n",
    "plt.axhline(y=8.994174, color='red')\n",
    "plt.ylim([0, 10])\n",
    "\n",
    "fig_savepath = './results/plots/mnist_1d.pdf'\n",
    "save_plot(fig_savepath)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
